<!DOCTYPE html>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script>
    var sameOrigin = 'http://127.0.0.1:8000';
    var crossOrigin = 'http://localhost:8000';
    var filePath = '/loading/resources/js-loaded.js?cross-origin';
    var jsLoaded = false;
    var loadSuccess = false;

    src = crossOrigin + filePath;
    document.write('<scr' + 'ipt src="' + src + '" onload="loadSuccess=true"></scr' + 'ipt>');
</script>

<script>
    test(function () {
        assert_true(jsLoaded);
        assert_true(loadSuccess);
    }, "Blocking of scripts doesn't come into effect since feature is disabled");
</script>
<script>
    if (window.internals) {
        internals.settings.setDisallowFetchForDocWrittenScriptsInMainFrameOnSlowConnections(true);
        internals.setNetworkConnectionInfoOverride(true, 'cellular2g', '4g', 100, 1.0);
        internals.evictAllResources();

        // Reset the state of the singleton network state notifier.
        window.addEventListener('beforeunload', function() {
            internals.settings.setDisallowFetchForDocWrittenScriptsInMainFrameOnSlowConnections(false);
            internals.clearNetworkConnectionInfoOverride();
        }, false);
    }

    filePath = '/loading/resources/js-loaded.js?same-origin';
    src = sameOrigin + filePath;
    jsLoaded = false;
    loadSuccess = false;
    document.write('<scr' + 'ipt src="' + src + '" onload="loadSuccess=true"></scr' + 'ipt>');
</script>

<script>
    test(function () {
        assert_true(jsLoaded);
        assert_true(loadSuccess);
    }, "Same domain doc.written scripts are not blocked");
</script>

<script>
    // The following tests are serialized and some steps are delayed by
    // step_timeout()s in order to deflake the tests, because the intervention
    // errors/warnings and the asynchronous request to the blocked script can
    // be made just after the corresponding <script>'s onerror/onload.

    var onload1;
    var onerror1;
    // Cross origin doc.written scripts are not blocked if cached.
    function testCachedCrossOriginScriptNotBlocked(t) {
        jsLoaded = false;
        var filePath = '/loading/resources/js-loaded.js?cross-origin';
        var src = crossOrigin + filePath;

        onload1 = t.step_func(function() {
            assert_true(jsLoaded,
                        "script should be executed since it is cached");
            if (window.internals) {
                header = internals.getResourceHeader(src, 'Intervention',
                                                     document);
                assert_equals(header,
                    '<https://www.chromestatus.com/feature/5718547946799104>; level="warning"',
                    'Intervention header');
            }

            t.step_timeout(testNonCachedCrossOriginScriptBlocked.bind(undefined, t), 0);
        });

        onerror1 = t.unreached_func(
            "script shouldn't be blocked since it is cached");

        t.step_timeout(function() {
            document.write('<scr' + 'ipt src="' + src +
                '" onload="onload1()" onerror="onerror1()"></scr' + 'ipt>');
        }, 1);
    }

    var onload2;
    var onerror2;
    // Cross origin doc.written scripts are blocked.
    function testNonCachedCrossOriginScriptBlocked(t) {
        jsLoaded = false;
        var filePath = '/loading/resources/js-loaded.js?cross-origin2';
        var src = crossOrigin + filePath;
        if (window.internals) {
            var originalPriority = internals.getInitialResourcePriority(src, document);
            var interventionPriority = originalPriority.then(() =>
                                  internals.getInitialResourcePriority(src, document));
        }

        onload2 = t.unreached_func(
            "non-cached cross origin doc.written script should be blocked");

        onerror2 = function() {
            t.step_timeout(function() {
                assert_false(jsLoaded,
                    "non-cached cross origin doc.written script " +
                    "shouldn't be executed");

                if (!window.internals) {
                  t.done();
                  return;
                }

                originalPriority.then(t.step_func(priority => {
                    assert_equals(priority, 3,
                        "The original request for the blocked script was " +
                        "sent with normal priority before it was intervened");
                    return interventionPriority;
                })).then(t.step_func(priority => {
                    assert_equals(priority, 0,
                        "The asynchronous request by the intervention to " +
                        "the blocked script was sent with the lowest " +
                        "priority");
                    t.done();
                }));
            }, 0);
        };

        document.write('<scr' + 'ipt src="' + src +
            '" onload="onload2()" onError="onerror2()"></scr' + 'ipt>');

        // Make sure that the intervention is not canceled by Oilpan GC.
        gc();
    }

    async_test(testCachedCrossOriginScriptNotBlocked, "cross-origin scripts");
</script>

<script>
</script>
